<?php
/*
 * 有问有答
 */

defined('ByShopWWI') or exit('Access Invalid!');
class ques_answerControl extends CMSHomeControl{

    public function __construct() {

        parent::__construct();
        Tpl::output('index_sign', 'ques_answer');
    }

    public function indexOp() {
        $model_article = Model('cms_article');

        $consult_id= $model_article->article_select('consult');
        $pay_id= $model_article->article_select('pay');
        $sale_id= $model_article->article_select('sale');
        $account_id= $model_article->article_select('account');
        $profit_id= $model_article->article_select('profit');



        //获取文章列表
        $page_number = 10;

        if(!isset($_GET['ques_answer']) | $_GET['ques_answer']== 'consult' ){
            $para= 'consult';
            $class_id= $model_article->article_select($para);
            $condition['article_class_id']= $class_id;
            Tpl::output('consult', 'on');
        }

        if(isset($_GET['ques_answer']) && $_GET['ques_answer']== 'pay' ){
            $para= $_GET['ques_answer'];
            $class_id= $model_article->article_select($para);
            $condition['article_class_id']= $class_id;
            Tpl::output('pay', 'on');
        }

        if(isset($_GET['ques_answer']) && $_GET['ques_answer']== 'sale' ){
            $para= $_GET['ques_answer'];
            $class_id= $model_article->article_select($para);
            $condition['article_class_id']= $class_id;
            Tpl::output('sale', 'on');
        }

        if(isset($_GET['ques_answer']) && $_GET['ques_answer']== 'account' ){
            $para= $_GET['ques_answer'];
            $class_id= $model_article->article_select($para);
            $condition['article_class_id']= $class_id;
            Tpl::output('account', 'on');
        }

        if(isset($_GET['ques_answer']) && $_GET['ques_answer']== 'profit' ){
            $para= $_GET['ques_answer'];
            $class_id= $model_article->article_select($para);
            $condition['article_class_id']= $class_id;
            Tpl::output('profit', 'on');
        }

        $bar=array();
        $page_bar=6;
        $bar['article_class_id'] = array('in', "$consult_id,$pay_id,$sale_id,$account_id,$profit_id");
        $bar['article_state'] = self::ARTICLE_STATE_PUBLISHED;
        $condition['article_state'] = self::ARTICLE_STATE_PUBLISHED;
        $article_list = $model_article->getList($condition, $page_number, 'article_sort asc, article_publish_time desc');
        Tpl::output('show_page', $model_article->showpage(2));
        $article_bar = $model_article->getList($bar, $page_bar, 'article_modify_time desc');
        Tpl::output('article_li', $article_list);
        Tpl::output('article_bar', $article_bar);
        Tpl::showpage('article_list_question');
    }

    /**
     * 文章列表
     */
    public function ques_answer_detailOp() {

        $para=array('consult','pay','sale','account','profit');
        $talk_id= $this->article_id($para);

        if(isset($_GET['article_id'])){
            $article_id = intval($_GET['article_id']);
            if(!in_array($article_id, $talk_id)) showMessage(Language::get('article_not_exist'),  CMS_SITE_QU_AN, '', 'error');

            if($article_id <= 0) {
                showMessage(Language::get('wrong_argument'),'','','error');
            }
        }else{
            $article_id= $talk_id[0];
        }

        $model_article = Model('cms_article');
        $article_class=  Model('cms_article_class');

        //购买咨询
        $consult_class_id= $model_article->article_select('consult');
        //付款及退款
        $pay_class_id= $model_article->article_select('pay');
        //售后规则
        $sale_class_id= $model_article->article_select('sale');
        //账户与资产
        $account_class_id= $model_article->article_select('account');
        //获利规则
        $profit_class_id= $model_article->article_select('profit');


        $article_detail = $model_article->getOne(array('article_id'=>$article_id));
        if(empty($article_detail)) {
            showMessage(Language::get('article_not_exist'), CMS_SITE_URL, '', 'error');
        }

        $article_title=$article_class->getList(array('class_id'=>$article_detail['article_class_id']), $page=null,
            $order='',$field='class_name');

        Tpl::output('class_name',$article_title[0]['class_name']);

        $class_class=$model_article->class_name($article_title[0]['class_name']);

        Tpl::output('class',$class_class);

        //计数加1
        $model_article->modify(array('article_click'=>array('exp','article_click+1')),array('article_id'=>$article_id));

        //文章心情
        $article_attitude_list = array();
        $article_attitude_list[1] = Language::get('attitude1');
        $article_attitude_list[2] = Language::get('attitude2');
        $article_attitude_list[3] = Language::get('attitude3');
        $article_attitude_list[4] = Language::get('attitude4');
        $article_attitude_list[5] = Language::get('attitude5');
        $article_attitude_list[6] = Language::get('attitude6');
        Tpl::output('article_attitude_list', $article_attitude_list);

        $condition=array();
        $condition['article_class_id'] = array('in',"$consult_class_id,$pay_class_id,$account_class_id,$profit_class_id,$sale_class_id");
        $ar_id_next= $model_article->articel_list($article_id, 'next',$condition);
        $ar_id_pre= $model_article->articel_list($article_id, 'pre',$condition);
        $bar_list=$model_article->bar_article(10,$condition);
        Tpl::output('id_next', $ar_id_next);
        Tpl::output('id_pre', $ar_id_pre);
        Tpl::output('bar_list', $bar_list);

        //分享
        $this->get_share_app_list();

        Tpl::output('article_detail', $article_detail);
        Tpl::output('detail_object_id', $article_id);
        Tpl::output('news', '最新问题');

        //seo
        Tpl::output('seo_title', $article_detail['article_title']);

        Tpl::showpage('ques_answer_detail');
    }

    private function article_id($para){
        $model_article = Model('cms_article');
        $condition_array= array();
        $article_id= array();
        $article_ids= array();

        foreach ($para as $value){
            $class_id=$model_article->article_select($value);
            array_push($article_ids, $class_id);
            $class_id= null;
        }

        $condition_array['article_class_id']= array('in', $article_ids);
        $para_id= $model_article->getList($condition_array, $page=null, $order='', $field='article_id', $limit='');

        foreach ($para_id as $value){
            array_push($article_id, $value['article_id']);
        }
        return $article_id;
    }

}